package com.liugui;

/**
 * @ClassName: YieldDemo
 * @Author: liugui
 * @Date: 2021-05-20 16:54
 **/
public class YieldDemo {
    public static void main(String[] args) {
        MyThread6 m6 = new MyThread6();
        Thread t1 = new Thread(m6,"测试线程α");
        Thread t2 = new Thread(m6,"测试线程β");

        t1.start();
        t2.start();
    }

}

class MyThread6 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "运行：" + i);

            if (i == 2){
                System.out.println("线程礼让：");
                Thread.yield();
            }
        }
    }
}